<template>
  <div class="app-container">
    <el-card>
      <el-tabs>
        <!-- 角色管理 -->
        <el-tab-pane label="角色管理">
          <el-row><el-button type="primary" style="margin-bottom:30px" @click="dialogVisible=true">新增角色</el-button></el-row>
          <!-- 表格 -->
          <el-table
            :data="roleList"
            border
            style="width: 100%"
          >
            <el-table-column
              prop="name"
              label="角色名称"
              width="240"
            />
            <el-table-column
              prop="description"
              label="描述"
            />
            <el-table-column
              label="操作"
            >
              <template slot-scope="scope">
                <el-button size="small" type="success" @click="assignPerm(scope.row.id)">分配权限</el-button>
                <el-button size="small" type="primary" @click="editFn(scope.row.id)">编辑</el-button>
                <el-button size="small" type="danger" @click="delFn(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-row type="flex" justify="center">
            <el-pagination
              style="margin-top:20px"
              layout="prev, pager, next"
              :page-size="pageConfig.pagesize"
              :total="total"
              @current-change="currentHandler"
            />
          </el-row>
        </el-tab-pane>
        <!-- 公司信息 -->
        <el-tab-pane label="公司信息">
          <el-form label-width="100px" style="margin-top:30px" disabled>
            <el-form-item label="公司名称" style="width:500px">
              <el-input v-model="companyInfo.name" />
            </el-form-item>
            <el-form-item label="公司地址" style="width:500px">
              <el-input v-model="companyInfo.companyAddress" />
            </el-form-item>
            <el-form-item label="邮箱" style="width:500px">
              <el-input v-model="companyInfo.mailbox" />
            </el-form-item>
            <el-form-item label="备注形式" style="width:500px">
              <el-input v-model="companyInfo.remarks" type="textarea" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <!-- 新增角色对话框,close关闭的回调函数，关闭有三种方式----取消按钮，x按钮，按超出对话框的地方-->
      <!-- title三元表达式判断，this.roleForm有id代表发送请求获取回来的数据 -->
      <el-dialog
        :title="roleForm.id?'编辑用户':'新增用户'"
        :visible.sync="dialogVisible"
        @close="cancelFn"
      >
        <el-form ref="ruleForm" :model="roleForm" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-button size="small" @click="cancelFn">取消</el-button>
          <el-button size="small" type="primary" @click="addRoleFn">确定</el-button>
        </el-row>
      </el-dialog>
    </el-card>
    <!-- 权限弹框 -->
    <el-dialog title="分配权限" :visible="showPerm" @close="showPerm=false">
      <el-checkbox-group v-model="permIds">
        <el-tree :data="permList" :props="{label:'name'}" default-expand-all :expand-on-click-node="false">
          <template v-slot="scope">
            <el-checkbox :label="scope.data.id">
              {{ scope.data.name }}
            </el-checkbox>
          </template>
        </el-tree>
      </el-checkbox-group>
      <template slot="footer">
        <el-row type="flex" justify="center">
          <el-button type="primary" size="small" @click="confirmPerm">确认</el-button>
          <el-button size="small" @click="showPerm=false">取消</el-button>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { addRoleList, getCompanyInfo, getRoleList, getRoleDetail, editRoleList, delRoleList, addPerm } from '@/api/setting'
import { renderTree } from '@/utils'
import { getPermList } from '@/api/permission'

export default {
  data() {
    return {
      // 用户列表
      roleList: [],
      // 公司详情
      companyInfo: {},
      pageConfig: {
        page: 1,
        pagesize: 4
      },
      total: 1,
      dialogVisible: false,
      // 新增/修改用户信息
      roleForm: {
        name: '',
        description: ''
      },
      permList: [],
      permIds: [],
      showPerm: false,
      // 设置输入框的内容的规则
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    // 获取公司详情
    this.companyInfo = await getCompanyInfo()
    console.log('公司详情', this.companyInfo)
    // 获取角色列表
    this.getRole()
  },
  methods: {
    async getRole() {
      const { rows, total } = await getRoleList(this.pageConfig)
      this.roleList = rows
      this.total = total
      console.log('用户列表', this.roleList)
    },
    // 分页
    currentHandler(val) {
      this.pageConfig.page = val
      this.getRole()
    },
    // 新增功能
    async addRoleFn() {
      try {
        // 判断是否通过正则,通过才进行下一步
        await this.$refs.ruleForm.validate()
        // 发送请求，判断是新增还是删除
        if (this.roleForm.id) {
          await editRoleList(this.roleForm)
        } else {
          await addRoleList(this.roleForm)
        }
        // 关闭弹窗
        this.dialogVisible = false
        // 提示
        this.$message.success('操作成功')
        // 重新获取列表
        this.getRole()
      } catch (error) {
        console.log(error)
      }
    },
    // 取消按钮
    cancelFn() {
      // 清除校验结果,并且要把数据清除
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.ruleForm.resetFields()
      this.dialogVisible = false
    },
    // 编辑功能
    async editFn(id) {
      this.dialogVisible = true
      this.roleForm = await getRoleDetail(id)
      console.log('新roleForm', this.roleForm)
    },
    // 删除功能
    async delFn(id) {
      try {
        await this.$confirm('确定删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消' })
        await delRoleList(id)
        // 提示
        this.$message.success('删除成功')
        // 如果删到没有长度，往前一页
        if (this.roleList.length === 1 && this.pageConfig.page > 1) {
          this.pageConfig.page--
        }
        // 重新获取列表
        this.getRole()
      } catch (error) {
        console.log(error)
      }
    },
    // 分配任务
    async assignPerm(id) {
      this.permList = renderTree(await getPermList(), '0')
      this.showPerm = true
      // 回显数据
      const res = await getRoleDetail(id)
      this.permIds = res.permIds
      this.roleID = id
    },
    // 确认
    async confirmPerm() {
      await addPerm({
        id: this.roleID,
        permIds: this.permIds
      })
      this.$message.success('操作成功')
      this.showPerm = false
    }
  }
}
</script>

<style>

</style>
